package org.nyt.medicalstore.repository;

import org.nyt.medicalstore.model.Drug;
import org.nyt.medicalstore.model.Symptom;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional;

public interface SymptomRepository extends JpaRepository<Symptom, Long> {

    Optional<Symptom> findByName(String name);
    //根据关键词查找
    @Query("SELECT s FROM Symptom s WHERE s.name LIKE CONCAT('%', :keyword, '%')")
    Page<Symptom> searchByName(@Param("keyword") String keyword, Pageable pageable);
    //根据症状查找药品
    @Query("SELECT DISTINCT d FROM Symptom s JOIN s.drugRelations r JOIN r.drug d WHERE s.id = :symptomId")
    List<Drug> findRelatedDrugs(@Param("symptomId") Long symptomId);
    //根据药品查找症状
    @Query("SELECT s FROM Symptom s JOIN s.drugRelations r JOIN r.drug d WHERE d.id = :drugId")
    List<Symptom> findRelatedSymptoms(@Param("drugId") Long drugId);

}
